package com.zengshuai.miniprogrambackend.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("wx")
public class WeChatCode2Session {
    @Autowired
    private RestTemplate restTemplate;//util配置
    @GetMapping("code2session")
    @ResponseBody
    public  String code2session(String code ) {
        System.out.println("code:"+code);
        // 调用接口
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("appid", "wxfcad8f696f98d628");
        paramMap.put("secret", "5dd022cd7c8b6188d74ec818018a9a85");
        paramMap.put("code", code);
        String result = this.restTemplate.getForObject(url, String.class, paramMap);
        System.out.println(result);
        return result;
    }
}

